package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.mapper.*;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.service.IWmsDeliveryPlanDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库计划货物信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-13
 */
@Service
public class WmsDeliveryPlanDetailServiceImpl implements IWmsDeliveryPlanDetailService
{
    @Autowired
    private WmsDeliveryPlanDetailMapper wmsDeliveryPlanDetailMapper;

    @Autowired
    private WmsCargoMapper wmsCargoMapper;

    @Autowired
    private WmsDeliveryRecordMapper wmsDeliveryRecordMapper;

    @Autowired
    private WmsDeliveryPlanMapper wmsDeliveryPlanMapper;

    @Autowired
    private WmsDeliveryRecordDetailMapper wmsDeliveryRecordDetailMapper;

    /**
     * 查询出库计划货物信息
     *
     * @param id 出库计划货物信息主键
     * @return 出库计划货物信息
     */
    @Override
    public WmsDeliveryPlanDetail selectWmsDeliveryPlanDetailById(Long id)
    {
        return wmsDeliveryPlanDetailMapper.selectWmsDeliveryPlanDetailById(id);
    }

    /**
     * 查询出库计划货物信息列表
     *
     * @param wmsDeliveryPlanDetail 出库计划货物信息
     * @return 出库计划货物信息
     */
    @Override
    public List<WmsDeliveryPlanDetail> selectWmsDeliveryPlanDetailList(WmsDeliveryPlanDetail wmsDeliveryPlanDetail)
    {
        return wmsDeliveryPlanDetailMapper.selectWmsDeliveryPlanDetailList(wmsDeliveryPlanDetail);
    }

    /**
     * 新增出库计划货物信息
     *
     * @param wmsDeliveryPlanDetail 出库计划货物信息
     * @return 结果
     */
    @Override
    public int insertWmsDeliveryPlanDetail(WmsDeliveryPlanDetail wmsDeliveryPlanDetail)
    {
        wmsDeliveryPlanDetail.setCreateTime(DateUtils.getNowDate());
        String cargoSn = wmsDeliveryPlanDetail.getCargoSn();
        WmsCargo wmsCargo = wmsCargoMapper.selectWmsCargoBySn(cargoSn);
        if (wmsCargo == null) {
            throw new ServiceException("没有查询到货物信息");
        }
        BeanUtils.copyProperties(wmsCargo, wmsDeliveryPlanDetail, "id", "remark", "createBy", "createTime", "updateBy", "updateTime");
        wmsDeliveryPlanDetail.setCargoId(wmsCargo.getId());
        int i = wmsDeliveryPlanDetailMapper.insertWmsDeliveryPlanDetail(wmsDeliveryPlanDetail);
        // 更新出库状态
        String deliveryStatus = "partial";
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo("CK" + wmsDeliveryPlanDetail.getDeliveryNo());
        Long planId = wmsDeliveryRecord.getPlanId();
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }

    /**
     * 修改出库计划货物信息
     *
     * @param wmsDeliveryPlanDetail 出库计划货物信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWmsDeliveryPlanDetail(WmsDeliveryPlanDetail wmsDeliveryPlanDetail)
    {
        WmsDeliveryPlanDetail old = wmsDeliveryPlanDetailMapper.selectWmsDeliveryPlanDetailById(wmsDeliveryPlanDetail.getId());
        wmsDeliveryPlanDetailMapper.deleteWmsDeliveryPlanDetailById(wmsDeliveryPlanDetail.getId());
        wmsDeliveryPlanDetail.setUpdateTime(DateUtils.getNowDate());
        String cargoSn = wmsDeliveryPlanDetail.getCargoSn();
        WmsCargo wmsCargo = wmsCargoMapper.selectWmsCargoBySn(cargoSn);
        if (wmsCargo == null) {
            throw new ServiceException("没有查询到货物信息");
        }
        BeanUtils.copyProperties(wmsCargo, wmsDeliveryPlanDetail, "id", "remark", "createBy", "createTime", "updateBy", "updateTime");
        wmsDeliveryPlanDetail.setCargoId(wmsCargo.getId());
        wmsDeliveryPlanDetail.setCreateBy(old.getCreateBy());
        wmsDeliveryPlanDetail.setCreateTime(old.getCreateTime());
        int i = wmsDeliveryPlanDetailMapper.insertWmsDeliveryPlanDetail(wmsDeliveryPlanDetail);
        // 更新出库状态
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo("CK" + wmsDeliveryPlanDetail.getDeliveryNo());
        Long planId = wmsDeliveryRecord.getPlanId();
        WmsDeliveryPlan plan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(planId);
        Integer status = wmsDeliveryRecordMapper.queryStatus(planId, plan.getDeliveryNo());
        String deliveryStatus = "partial";
        if (status == null || status == 0) {
            deliveryStatus = "all";
        }
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }

    /**
     * 批量删除出库计划货物信息
     *
     * @param ids 需要删除的出库计划货物信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryPlanDetailByIds(Long[] ids)
    {
        int i = 0;
        for (Long id : ids) {
            i += deleteWmsDeliveryPlanDetailById(id);
        }
        return i;
    }

    /**
     * 删除出库计划货物信息信息
     *
     * @param id 出库计划货物信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryPlanDetailById(Long id)
    {
        WmsDeliveryPlanDetail planDetail = wmsDeliveryPlanDetailMapper.selectWmsDeliveryPlanDetailById(id);
        WmsDeliveryPlan plan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanByDeliveryNo(planDetail.getDeliveryNo());
        Long planId = plan.getId();
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByPlanId(planId);
        int i = wmsDeliveryPlanDetailMapper.deleteWmsDeliveryPlanDetailById(id);
        String lotNo = planDetail.getLotNo();
        String cargoSn = planDetail.getCargoSn();
        String planDeliveryNo = planDetail.getDeliveryNo();
        wmsDeliveryRecordDetailMapper.deleteWmsDeliveryRecordDetailByLotAndCargo(lotNo, cargoSn, "CK" + planDeliveryNo);
        // 更新出库状态
        WmsDeliveryRecordDetail query = new WmsDeliveryRecordDetail();
        query.setDeliveryNo(wmsDeliveryRecord.getDeliveryNo());
        List<WmsDeliveryRecordDetail> details = wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetailList(query);
        String deliveryStatus = "partial";
        if (ObjectUtils.isEmpty(details)) {
            deliveryStatus = "wait";
        }
        Integer status = wmsDeliveryRecordMapper.queryStatus(planId, planDetail.getDeliveryNo());
        if (status == null || status == 0) {
            deliveryStatus = "all";
        }
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }
}
